package com.dao;

import com.model.Option;
import com.model.Question;

import java.sql.*;
import java.util.ArrayList;

public class QuestionDaoImpl implements QuestionDao {
    @Override
    public ArrayList<Question> getQuestions(int n) {
        String sql = "SELECT *\n" +
                "FROM questions\n" +
                "LIMIT " + n;
        ArrayList<Question> questions = new ArrayList<Question>();
        try (Connection connection = getConnection();
             Statement statement = connection.createStatement();
             ResultSet resultSet = statement.executeQuery(sql)) {
            while (resultSet.next()) {
                Question question = new Question();
                question.setqId(resultSet.getInt("q_id"));
                question.setContext(resultSet.getString("context"));
                question.setType(resultSet.getInt("type"));
                int qId = resultSet.getInt("q_id");
                ArrayList<Option> options = null;
                options = getOptionsByQId(qId);
                question.setOptions(options);
                ArrayList<Option> answers = null;
                answers = getAnswersByQId(qId);
                question.setAnswers(answers);
                questions.add(question);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return questions;
    }

    @Override
    public ArrayList<Option> getOptionsByQId(int qId) {
        String sql = "SELECT  *\n" +
                "FROM `options`\n" +
                "WHERE q_id=?;";
        ArrayList<Option> options = new ArrayList<Option>();
        try (Connection connection = getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            preparedStatement.setInt(1, qId);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                Option option = new Option();
                option.setoId(resultSet.getInt("o_id"));
                option.setContext(resultSet.getString("context"));
                options.add(option);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return options;
    }

    @Override
    public ArrayList<Option> getAnswersByQId(int qId) {
        String sql = "SELECT  *\n" +
                "FROM `options`\n" +
                "WHERE ans_q_id=?;";
        ArrayList<Option> answers = new ArrayList<Option>();
        try (Connection connection = getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            preparedStatement.setInt(1, qId);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                Option option = new Option();
                option.setoId(resultSet.getInt("o_id"));
                option.setContext(resultSet.getString("context"));
                answers.add(option);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return answers;
    }

    @Override
    public boolean isRightAns(String[] userOptionId, int qId) {
        String sql = "SELECT *\n" +
                "FROM `options`\n" +
                "WHERE ans_q_id=?;";
        int type = getQuestionType(qId);
        if (type == 3) {
            userOptionId = contextToOId(userOptionId[0]);
        }
        try (Connection connection = getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            preparedStatement.setInt(1, qId);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                int i = 0;
                boolean isFound = false;
                for (String oneOfOId : userOptionId) {
                    if (oneOfOId != null) {
                        int OId = Integer.parseInt(oneOfOId);
                        if (resultSet.getInt("o_id") == OId) {
                            isFound = true;
                            break;
                        }
                    }
                }
                if (isFound == false) {
                    return false;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return true;
    }

    @Override
    public int getQuestionRealNum() {
        int n = 0;
        String sql = "SELECT COUNT(*)\n" +
                "FROM questions;";
        try (Connection connection = getConnection();
             Statement statement = connection.createStatement();
             ResultSet resultSet = statement.executeQuery(sql)) {
            while (resultSet.next()) {
                n = resultSet.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return n;
    }

    @Override
    public int getQuestionType(int qId) {
        String sql = "SELECT type\n" +
                "FROM questions\n" +
                "WHERE q_id=?;";
        try (Connection connection = getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            preparedStatement.setInt(1, qId);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()) {
                return resultSet.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return -1;
    }

    @Override
    public String[] contextToOId(String context) {
        String sql = "SELECT o_id\n" +
                "FROM `options`\n" +
                "WHERE context=?;";
        String[] oids = new String[10];
        try (Connection connection = getConnection();
             PreparedStatement preparedStatement = connection.prepareStatement(sql)) {
            preparedStatement.setString(1, context);
            ResultSet resultSet = preparedStatement.executeQuery();
            int size = 0;
            while (resultSet.next()) {
                int oid = resultSet.getInt(1);
                oids[size] = String.valueOf(oid);
                size++;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return oids;
    }
}
